package com.jy.appmarket.cards;

import java.io.File;
import java.lang.ref.WeakReference;

import android.content.Context;
import android.view.View;
import android.widget.ListView;

import com.jy.app.market.idata.data.Apk;
import com.jy.appmarket.download.DownloadActivity.AppAndDownloadListener;
import com.jy.appmarket.download.DownloadItem;
import com.jy.appmarket.download.DownloadUtils;
import com.jy.appmarket.statistics.ApkSource;
import com.jy.appmarket.utils.UITools;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public abstract class DownloadableUICard extends BaseUICard implements AppAndDownloadListener {
	private ApkSource apkSource;

	public DownloadableUICard(Context context) {
		super(context);
	}

	public DownloadableUICard(Context context, int innerLayout) {
		super(context, innerLayout);
	}

	// protected int getProgress(DownloadItem di) {
	// if (di == null) {
	// return 0;
	// }
	// return getProgress(di.getTotalSize(), di.getDownloadedSize());
	// }

	protected int getProgress(DownloadItem di, Apk apk, String dlId) {
		if (di != null) {
			return getProgress(di.getTotalSize(), di.getDownloadedSize());
		} else {
			long totalSize = apk.getBytes().longValue();
			long downloadedSize = getDownloadedFileSizeById(dlId);
			return getProgress(totalSize, downloadedSize);
		}
	}

	protected int getProgress(long totalSize, long downloadedSize) {

		long total = totalSize;
		if (total == 0) {
			total = Long.MAX_VALUE;
		}

		int per = (int) (downloadedSize * 100.0f / total);

		if (per > 100) {
			per = 100;
		} else if (per < 0) {
			per = 0;
		}
		return per;
	}

	// protected String getProgressString(DownloadItem di) {
	// if(di == null) {
	// return "";
	// }
	// return getProgressString(di.getTotalSize(), di.getDownloadedSize());
	// }

	protected String getProgressString(DownloadItem di, Apk apk, String dlId) {
		if (di != null) {
			return getProgressString(di.getTotalSize(), di.getDownloadedSize());
		} else {
			long totalSize = apk.getBytes().longValue();
			long downloadedSize = getDownloadedFileSizeById(dlId);
			return getProgressString(totalSize, downloadedSize);
		}
	}

	protected String getProgressString(long totalSize, long downloadedSize) {
		if (totalSize == 0) {
			return "";
		}

		String totalSizeStr = UITools.normalizationFileSize(totalSize);

		String dlSize = null;
		if (totalSizeStr.contains("M")) {
			dlSize = UITools.normalizationFileSizeToM(downloadedSize, false);
		} else {
			dlSize = UITools.normalizationFileSizeToK(downloadedSize, false);
		}

		return dlSize + "/" + totalSizeStr;
	}

	protected String getSpeedString(DownloadItem di) {
		if (di == null) {
			return "0K/S";
		}

		return getSpeedString(di.getSpeed());
	}

	protected String getSpeedString(long speed) {
		if (speed <= 0) {
			return "0K/S";
		}

		if (speed > 0 && speed < 1024 * 1024) {
			float kb = speed / 1024.0f;
			String skb = String.format("%.1f", kb) + "K/S";
			return skb;
		} else {
			float mb = speed / (1024.0f * 1024);
			String smb = String.format("%.1f", mb) + "M/S";
			return smb;
		}
	}

	protected long getDownloadedFileSizeById(String dlId) {
		String filePath = DownloadUtils.getDownloadApkTmpFilePathById(dlId);
		File f = new File(filePath);
		return f.length();
	}

	public ApkSource getApkSource() {
		return apkSource;
	}

	public void setApkSource(ApkSource apkSource) {
		this.apkSource = apkSource;
	}

	// private WeakReference<BaseAdapter> mAdapter;
	//
	// public void setAdapter(BaseAdapter adapter) {
	// if (adapter != null) {
	// mAdapter = new WeakReference<BaseAdapter>(adapter);
	// }
	// }
	//
	// public void refreshList() {
	// if (mAdapter != null && mAdapter.get() != null) {
	// mAdapter.get().notifyDataSetChanged();
	// }
	// }

	public boolean refreshList(int position) {
		position++;
		if (mListView != null && mListView.get() != null) {
			final ListView listview = mListView.get();
			int first = listview.getFirstVisiblePosition();
			int last = listview.getLastVisiblePosition();

			if (position >= first && position <= last) {
				View view = listview.getChildAt(position - first);
				listview.getAdapter().getView(position, view, listview);
			}
			return true;
		}
		return false;
	}

	private WeakReference<ListView> mListView;

	public void setListView(ListView listview) {
		if (listview != null) {
			mListView = new WeakReference<ListView>(listview);
		}
	}

	public static int safeInt(Integer val) {
		return val == null ? 0 : val.intValue();
	}
}